#!/bin/sh
# PCP QA Test No. 1788
# Exercise Linux kernel hugepage sysfs metrics.
#
# Copyright (c) 2024-2025 Red Hat.  All Rights Reserved.
#

seq=`basename $0`
echo "QA output created by $seq"

# get standard environment, filters and checks
. ./common.product
. ./common.filter
. ./common.check

[ $PCP_PLATFORM = linux ] || _notrun "Linux-specific memory metric testing"

_cleanup()
{
    cd $here
    $sudo rm -f $PCP_VAR_DIR/config/pmda/60.{43,44}
    [ -f $PCP_VAR_DIR/config/pmda/60.43.$seq ] && _restore_config $PCP_VAR_DIR/config/pmda/60.43
    [ -f $PCP_VAR_DIR/config/pmda/60.44.$seq ] && _restore_config $PCP_VAR_DIR/config/pmda/60.44
    $sudo rm -rf $tmp $tmp.*
}

status=1	# failure is the default!
trap "_cleanup; exit \$status" 0 1 2 3 15

# deterministic output - move aside any existing indom caches
[ -f $PCP_VAR_DIR/config/pmda/60.43 ] && _save_config $PCP_VAR_DIR/config/pmda/60.43
[ -f $PCP_VAR_DIR/config/pmda/60.44 ] && _save_config $PCP_VAR_DIR/config/pmda/60.44

_filter_instances()
{
    $here/src/sortinst | sed -e 's/inst \[./inst [N/g'
}

# real QA test starts here
root=$tmp.root
export LINUX_STATSPATH=$root
pmda=$PCP_PMDAS_DIR/linux/pmda_linux.so,linux_init
local="-L -K clear -K add,60,$pmda"

metrics=`pminfo $local mem.hugepages mem.numa.hugepages | LC_COLLATE=POSIX sort`
for tgz in $here/linux/sysfs-hugepages-*.tgz
do
    rm -fr $root
    mkdir $root || _fail "root in use when processing $tgz"
    cd $root
    tar xzf $tgz
    base=`basename $tgz`

    echo "== Checking hugepages metric values - $base"
    pminfo $local -f $metrics | _filter_instances
    echo && echo "== done" && echo

    cd $here
done

# success, all done
status=0
exit
